package com.dingwei.superiordiary.adapter.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.bean.ShopCartListBean;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created time 2018/5/7.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */
public class ShopCarAdapter extends RecyclerArrayAdapter<ShopCartListBean.DataBean> {
    public interface onShopCartInterFace {
        void add(int storePosition, int position);

        void reduce(int storePosition, int position);

        void check(int storePosition, int position);

        void delete(int storePosition, int position);

        void storeCheck(int position);
    }

    private onShopCartInterFace onShopCartInterFace;

    public void setOnShopCartInterFaceListener(onShopCartInterFace listener) {
        onShopCartInterFace = listener;

    }

    public ShopCarAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new ShopCarHolder(parent);
    }

    class ShopCarHolder extends BaseViewHolder<ShopCartListBean.DataBean> {


        private ShopCartDetailAdapter adapter;
        @Bind(R.id.img_check)
        ImageView imgCheck;
        @Bind(R.id.img_business)
        ImageView imgBusiness;
        @Bind(R.id.tv_business)
        TextView tvBusiness;
        @Bind(R.id.m_recyclerView)
        RecyclerView mRecyclerView;

        public ShopCarHolder(ViewGroup parent) {
            super(parent, R.layout.item_shopcart);
            ButterKnife.bind(this, itemView);

            LinearLayoutManager layoutManager = new LinearLayoutManager(BaseActivity.context);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            mRecyclerView.setLayoutManager(layoutManager);
            mRecyclerView.setNestedScrollingEnabled(false);
            adapter = new ShopCartDetailAdapter(BaseActivity.context);
            mRecyclerView.setAdapter(adapter);
            mRecyclerView.setFocusable(false);

        }

        @Override
        public void setData(ShopCartListBean.DataBean data) {
            super.setData(data);
            tvBusiness.setText(data.store_name);
            adapter.clear();
            adapter.addAll(data.products);
            if (data.isSelect) {
                imgCheck.setImageResource(R.drawable.ic_sele);
            } else {
                imgCheck.setImageResource(R.drawable.ic_unsele);
            }

            adapter.setOnShopCartDetailInterFaceListener(new ShopCartDetailAdapter.onShopCartDetailInterFace() {
                @Override
                public void add(int position) {
                    if (onShopCartInterFace!=null){
                        onShopCartInterFace.add(getDataPosition(),position);
                    }
                }

                @Override
                public void reduce(int position) {
                    if (onShopCartInterFace!=null){
                        onShopCartInterFace.reduce(getDataPosition(),position);
                    }
                }

                @Override
                public void check(int position) {
                    if (onShopCartInterFace!=null){
                        onShopCartInterFace.check(getDataPosition(),position);
                    }
                }

                @Override
                public void delete(int position) {
                    if (onShopCartInterFace!=null){
                        onShopCartInterFace.delete(getDataPosition(),position);
                    }
                }
            });
            imgCheck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onShopCartInterFace!=null){
                        onShopCartInterFace.storeCheck(getDataPosition());
                    }
                }
            });
        }
    }
}
